package com.alperez.androidsmscamera.model;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

/**
 * This class encapsulates description of user accounts along with the expiration date of the
 * account.
 * This class is suitable for account with types of DEFINED_USER and ROOT_USER
 * @author Stas
 *
 */
public class AccountInfo {
	private int accountId;
	private long expirationDate; // if 0 - the account is forever
	private String firstName;
	private String lastName;
	private String email;
	
	public AccountInfo() {}
	
	/**
	 * Copy constructor
	 * @param aInfo
	 */
	public AccountInfo(AccountInfo aInfo) {
		if (aInfo == null) {
			throw new IllegalArgumentException("Provided AccountInfo for copy constructor can not be null");
		}
		this.accountId = aInfo.accountId;
		this.expirationDate = aInfo.expirationDate;
		if (aInfo.firstName != null) {
			this.firstName = new String(aInfo.firstName);
		}
		if (aInfo.lastName != null) {
			this.lastName = new String(aInfo.lastName);
		}
		if (aInfo.email != null) {
			this.email = new String(aInfo.email);
		}
	}
	
	public int getAccountId() {
		return accountId;
	}
	
	public void setAccountId(int accountId) {
		this.accountId = accountId;
	}

	public long getExpirationDate() {
		return expirationDate;
	}

	public void setExpirationDate(long expirationDate) {
		this.expirationDate = expirationDate;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	public JSONObject toJSONObject() throws JSONException {
		JSONObject ret = new JSONObject();
		ret.put("accountId", accountId);
		ret.put("expirationDate", expirationDate);
		if (firstName != null) ret.put("firstName", firstName);
		if (lastName != null) ret.put("lastName", lastName);
		if (email != null) ret.put("email", email);
		return ret;
	}
	
	public String toJson() throws JSONException {
		return toJSONObject().toString();
	}
	
	public void fromJson(String strJson) throws JSONException {
		JSONObject jObj = new JSONObject(strJson);
		int accId = jObj.getInt("accountId");
		long exp = jObj.getLong("expirationDate");
		this.accountId = accId;
		this.expirationDate = exp;
		this.firstName = jObj.optString("firstName", null);
		this.lastName = jObj.optString("lastName", null);
		this.email = jObj.optString("email", null);
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder("{accountId="+accountId+", expired=");
		sb.append((expirationDate == 0) ? "no expiration" : String.format("%1$tF - %1$tT", new Date(expirationDate)));
		if (!TextUtils.isEmpty(firstName)) {
			sb.append(", firstName="+firstName);
		}
		if (!TextUtils.isEmpty(lastName)) {
			sb.append(", lastName="+lastName);
		}
		if (!TextUtils.isEmpty(email)) {
			sb.append(", email="+email);
		}
		return sb.toString();
	}
}
